<html>
<head><meta charset="utf-8"><title>printing an hir::Expr · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/printing.20an.20hir.3A.3AExpr.html">printing an hir::Expr</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="209947132"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/printing%20an%20hir%3A%3AExpr/near/209947132" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matthiaskrgr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/printing.20an.20hir.3A.3AExpr.html#209947132">(Sep 13 2020 at 19:55)</a>:</h4>
<p>hi, how do I print an hir::Expr? I want to print the code that it represents, how do I do that?</p>



<a name="209947263"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/printing%20an%20hir%3A%3AExpr/near/209947263" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matthiaskrgr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/printing.20an.20hir.3A.3AExpr.html#209947263">(Sep 13 2020 at 19:59)</a>:</h4>
<p>(it's for an error message)</p>



<a name="209947274"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/printing%20an%20hir%3A%3AExpr/near/209947274" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/printing.20an.20hir.3A.3AExpr.html#209947274">(Sep 13 2020 at 19:59)</a>:</h4>
<p>for an error message I would use a diagnostic instead</p>



<a name="209947277"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/printing%20an%20hir%3A%3AExpr/near/209947277" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/printing.20an.20hir.3A.3AExpr.html#209947277">(Sep 13 2020 at 19:59)</a>:</h4>
<p><a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/struct.Diagnostic.html">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/struct.Diagnostic.html</a></p>



<a name="209947289"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/printing%20an%20hir%3A%3AExpr/near/209947289" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/printing.20an.20hir.3A.3AExpr.html#209947289">(Sep 13 2020 at 19:59)</a>:</h4>
<p><a href="https://rustc-dev-guide.rust-lang.org/diagnostics.html">https://rustc-dev-guide.rust-lang.org/diagnostics.html</a></p>



<a name="209947378"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/printing%20an%20hir%3A%3AExpr/near/209947378" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/printing.20an.20hir.3A.3AExpr.html#209947378">(Sep 13 2020 at 20:01)</a>:</h4>
<p>Can you use its <code>span</code>?</p>



<a name="209947448"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/printing%20an%20hir%3A%3AExpr/near/209947448" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matthiaskrgr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/printing.20an.20hir.3A.3AExpr.html#209947448">(Sep 13 2020 at 20:02)</a>:</h4>
<p>well I need that for building the suggestion</p>



<a name="209947467"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/printing%20an%20hir%3A%3AExpr/near/209947467" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matthiaskrgr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/printing.20an.20hir.3A.3AExpr.html#209947467">(Sep 13 2020 at 20:03)</a>:</h4>
<p>I wondered about <code>span</code> too, but I was not sure how to convert it into DIsplay or &amp;str</p>



<a name="209947542"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/printing%20an%20hir%3A%3AExpr/near/209947542" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/printing.20an.20hir.3A.3AExpr.html#209947542">(Sep 13 2020 at 20:05)</a>:</h4>
<p>it might help to look at an existing example: <a href="https://github.com/rust-lang/rust/pull/76143/files#diff-0d908c5802dbf98407b95601565449f6R1087">https://github.com/rust-lang/rust/pull/76143/files#diff-0d908c5802dbf98407b95601565449f6R1087</a></p>



<a name="209947584"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/printing%20an%20hir%3A%3AExpr/near/209947584" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/printing.20an.20hir.3A.3AExpr.html#209947584">(Sep 13 2020 at 20:06)</a>:</h4>
<p><span class="user-mention silent" data-user-id="232545">Joshua Nelson</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/printing.20an.20hir.3A.3AExpr/near/209947542">said</a>:</p>
<blockquote>
<p>it might help to look at an existing example: <a href="https://github.com/rust-lang/rust/pull/76143/files#diff-0d908c5802dbf98407b95601565449f6R1087">https://github.com/rust-lang/rust/pull/76143/files#diff-0d908c5802dbf98407b95601565449f6R1087</a></p>
</blockquote>
<p>I believe the problem is concatenating that <code>span</code> with something else?</p>



<a name="209947599"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/printing%20an%20hir%3A%3AExpr/near/209947599" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/printing.20an.20hir.3A.3AExpr.html#209947599">(Sep 13 2020 at 20:06)</a>:</h4>
<p>well if you have a span the diagnostic will print the source code for you</p>



<a name="209947602"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/printing%20an%20hir%3A%3AExpr/near/209947602" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/printing.20an.20hir.3A.3AExpr.html#209947602">(Sep 13 2020 at 20:06)</a>:</h4>
<p>you don't need to stringify the HIR or anything like that</p>



<a name="209947679"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/printing%20an%20hir%3A%3AExpr/near/209947679" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/printing.20an.20hir.3A.3AExpr.html#209947679">(Sep 13 2020 at 20:09)</a>:</h4>
<p>Otherwise there's <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir_pretty/index.html"><code>rustc_hir_pretty</code></a>, not sure if that's relevant</p>



<a name="209947684"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/printing%20an%20hir%3A%3AExpr/near/209947684" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matthiaskrgr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/printing.20an.20hir.3A.3AExpr.html#209947684">(Sep 13 2020 at 20:09)</a>:</h4>
<p><code>        let sugg = format!("{}.insert({:?}, {})", base_string_snippet, lit, extension_string);</code></p>



<a name="209947686"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/printing%20an%20hir%3A%3AExpr/near/209947686" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/printing.20an.20hir.3A.3AExpr.html#209947686">(Sep 13 2020 at 20:09)</a>:</h4>
<p><span class="user-mention silent" data-user-id="232545">Joshua Nelson</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/printing.20an.20hir.3A.3AExpr/near/209947599">said</a>:</p>
<blockquote>
<p>well if you have a span the diagnostic will print the source code for you</p>
</blockquote>
<p>Though you won't be able to tweak it</p>



<a name="209947688"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/printing%20an%20hir%3A%3AExpr/near/209947688" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/printing.20an.20hir.3A.3AExpr.html#209947688">(Sep 13 2020 at 20:09)</a>:</h4>
<p>oh I see you want a structured suggestion</p>



<a name="209947691"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/printing%20an%20hir%3A%3AExpr/near/209947691" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/printing.20an.20hir.3A.3AExpr.html#209947691">(Sep 13 2020 at 20:09)</a>:</h4>
<p>yeah not sure about those, sorry</p>



<a name="209947697"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/printing%20an%20hir%3A%3AExpr/near/209947697" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matthiaskrgr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/printing.20an.20hir.3A.3AExpr.html#209947697">(Sep 13 2020 at 20:10)</a>:</h4>
<p>where the <code>:?</code> is, I'm trying to put the second function arg which I can get via <code>&amp;args[1]</code></p>



<a name="209956847"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/printing%20an%20hir%3A%3AExpr/near/209956847" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/printing.20an.20hir.3A.3AExpr.html#209956847">(Sep 14 2020 at 00:40)</a>:</h4>
<p><span class="user-mention silent" data-user-id="217864">matthiaskrgr</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/printing.20an.20hir.3A.3AExpr/near/209947467">said</a>:</p>
<blockquote>
<p>I wondered about <code>span</code> too, but I was not sure how to convert it into DIsplay or &amp;str</p>
</blockquote>
<p>I don't know if this is relevant to what you're asking about, but there's <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_span/source_map/struct.SourceMap.html#method.span_to_snippet"><code>span_to_snippet</code></a>.</p>



<a name="210670081"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/printing%20an%20hir%3A%3AExpr/near/210670081" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/printing.20an.20hir.3A.3AExpr.html#210670081">(Sep 20 2020 at 14:50)</a>:</h4>
<p><span class="user-mention" data-user-id="217864">@matthiaskrgr</span> Did you solve this, and if so can you explain me how? I have a similar need <span aria-label="grinning face with smiling eyes" class="emoji emoji-1f601" role="img" title="grinning face with smiling eyes">:grinning_face_with_smiling_eyes:</span></p>



<a name="210670253"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/printing%20an%20hir%3A%3AExpr/near/210670253" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matthiaskrgr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/printing.20an.20hir.3A.3AExpr.html#210670253">(Sep 20 2020 at 14:54)</a>:</h4>
<p>uh, I just used the span.. <code>args[0].span</code></p>



<a name="210670269"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/printing%20an%20hir%3A%3AExpr/near/210670269" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matthiaskrgr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/printing.20an.20hir.3A.3AExpr.html#210670269">(Sep 20 2020 at 14:55)</a>:</h4>
<div class="codehilite"><pre><span></span><code>        let pos_arg = snippet(cx, args[1].span, &quot;..&quot;);
        let sugg = format!(&quot;{}.insert({}, {})&quot;, base_string_snippet, pos_arg, extension_string);
</code></pre></div>



<a name="210670271"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/printing%20an%20hir%3A%3AExpr/near/210670271" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matthiaskrgr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/printing.20an.20hir.3A.3AExpr.html#210670271">(Sep 20 2020 at 14:55)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust-clippy/pull/6037/files#diff-76448e132e544f8a1ff89ffa05ef9566R3350">https://github.com/rust-lang/rust-clippy/pull/6037/files#diff-76448e132e544f8a1ff89ffa05ef9566R3350</a></p>



<a name="210670331"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/printing%20an%20hir%3A%3AExpr/near/210670331" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matthiaskrgr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/printing.20an.20hir.3A.3AExpr.html#210670331">(Sep 20 2020 at 14:56)</a>:</h4>
<p>(Expr.span basically)  <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/struct.Expr.html">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/struct.Expr.html</a></p>



<a name="210673310"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/printing%20an%20hir%3A%3AExpr/near/210673310" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/printing.20an.20hir.3A.3AExpr.html#210673310">(Sep 20 2020 at 16:15)</a>:</h4>
<p>Got it, thanks!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>